package com.shenda.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.shenda.api.dao.LogDao;
import com.shenda.api.entity.Log;
import com.shenda.api.service.LogService;
import com.swak.annotation.FluxService;
import com.swak.entity.Page;
import com.swak.entity.Parameters;
import com.swak.persistence.QueryCondition;
import com.swak.persistence.QueryCondition.Criteria;
import com.swak.utils.StringUtils;

/**
 * 操作日志
 * 
 * @author lifeng
 */
@FluxService
public class LogServiceImpl implements LogService {

	@Autowired
	private LogDao logDao;

	/**
	 * 分页查询
	 */
	@Override
	public Page page(Log query, Parameters param) {
		QueryCondition qc = new QueryCondition();
		Criteria criteria = qc.getCriteria();
		if (StringUtils.isNotBlank(query.getGyro())) {
			criteria.andEqualTo("GYRO", query.getGyro());
		}
		qc.setOrderByClause("OPSTIME DESC, OPSTYPE ASC");
		Page page = logDao.page(qc, param);
		List<Log> logs = page.getData();
		for (Log log : logs) {
			log.setGyro(log.crc16(log.getGyro(), true));
		}
		return page;
	}

	/**
	 * 查询数据
	 */
	@Override
	public void save(Log log) {
		this.logDao.insert(log);
	}

	/**
	 * 最新消息数量
	 */
	@Override
	public Integer news(Log query) {
		QueryCondition qc = new QueryCondition();
		Criteria c = qc.getCriteria();
		c.andGreaterThanOrEqualTo("OPSTIME", query.getOpsTime());
		return this.logDao.count(qc);
	}
}
